﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace GatoRatoQueijo
{
    class Missile : Objeto2D
    {
        public float velocidadeX, velocidadeY;
        private GameWindow window;
        private float vel;
        private float aceleration;

        static List<Missile> listaMisseis = new List<Missile>();

        public Missile(Texture2D textura, GameWindow window, Nullable<Rectangle> sourceRectangle, float depth, Vector2 shooterPosition, Vector2 TargetPosition, float shooterRotation, String pos)
            : base(textura, sourceRectangle, depth)
        {
            this.window = window;
            this.x = shooterPosition.X;
            this.y = shooterPosition.Y;

            ScaleX = ScaleY = 0.6f;


            vel = -6;

            aceleration = 0.35f;

            double angleRadian = Math.Atan2((double)(this.y - TargetPosition.Y), (double)(this.x - TargetPosition.X));
            double angleDegree = angleRadian * 180 / Math.PI;

            if (angleDegree < 0)
            {
                angleDegree = angleDegree + 360;
            }

            angleDegree -= 180;

            float angulo = (float)angleDegree;

            this.rotation = angulo;

            if ((shooterRotation > -90) && (shooterRotation < 90) && (pos == "cima"))
            {
                pos = "baixo";
            }
            else if ((shooterRotation > -90) && (shooterRotation < 90) && (pos == "baixo"))
            {
                pos = "cima";
            }


            if (pos == "baixo")
            {
                this.x += (float)(Math.Cos(((this.rotation - 140) * Math.PI) / 180) * 40);
                this.y += (float)(Math.Sin(((this.rotation - 140) * Math.PI) / 180) * 40);
            }
            else if (pos == "cima")
            {
                this.x += (float)(Math.Cos(((this.rotation + 140) * Math.PI) / 180) * 40);
                this.y += (float)(Math.Sin(((this.rotation + 140) * Math.PI) / 180) * 40);
            }

            angleRadian = Math.Atan2((double)(this.y - TargetPosition.Y), (double)(this.x - TargetPosition.X));
            angleDegree = angleRadian * 180 / Math.PI;

            if (angleDegree < 0)
            {
                angleDegree = angleDegree + 360;
            }

            angleDegree -= 180;

            angulo = (float)angleDegree;

            this.rotation = angulo;
        }

        public static void CriarMissile(Texture2D textura, GameWindow window, float depth, Vector2 shooterPosition, Vector2 TargetPosition, float shooterRotation)
        {
            Random rnd = new Random();

            float missileType = rnd.Next(6) + 1;
            missileType = 4;

            if (missileType == 1)
            {
                Rectangle sourceRectangle = new Rectangle(0, 0, 90, 50);

                Missile missel = new Missile(textura, window, sourceRectangle, depth, shooterPosition, TargetPosition, shooterRotation, "cima");
                listaMisseis.Add(missel);

                sourceRectangle = new Rectangle(90, 0, 235, 50);

                Missile missel2 = new Missile(textura, window, sourceRectangle, depth, shooterPosition, TargetPosition, shooterRotation, "baixo");
                listaMisseis.Add(missel2);
            }
            else if (missileType == 2)
            {
                Rectangle sourceRectangle = new Rectangle(0, 60, 140, 50);

                Missile missel = new Missile(textura, window, sourceRectangle, depth, shooterPosition, TargetPosition, shooterRotation, "cima");
                listaMisseis.Add(missel);

                sourceRectangle = new Rectangle(140, 60, 100, 50);

                Missile missel2 = new Missile(textura, window, sourceRectangle, depth, shooterPosition, TargetPosition, shooterRotation, "baixo");
                listaMisseis.Add(missel2);
            }
            else if (missileType == 3)
            {
                Rectangle sourceRectangle = new Rectangle(0, 120, 140, 50);

                Missile missel = new Missile(textura, window, sourceRectangle, depth, shooterPosition, TargetPosition, shooterRotation, "cima");
                listaMisseis.Add(missel);

                sourceRectangle = new Rectangle(140, 120, 220, 50);

                Missile missel2 = new Missile(textura, window, sourceRectangle, depth, shooterPosition, TargetPosition, shooterRotation, "baixo");
                listaMisseis.Add(missel2);
            }
            else if (missileType == 4)
            {
                Rectangle sourceRectangle = new Rectangle(0, 170, 225, 50);

                Missile missel = new Missile(textura, window, sourceRectangle, depth, shooterPosition, TargetPosition, shooterRotation, "cima");
                listaMisseis.Add(missel);

                sourceRectangle = new Rectangle(225, 170, 175, 50);

                Missile missel2 = new Missile(textura, window, sourceRectangle, depth, shooterPosition, TargetPosition, shooterRotation, "baixo");
                listaMisseis.Add(missel2);
            }
            else if (missileType == 5)
            {
                Rectangle sourceRectangle = new Rectangle(0, 60, 140, 50);

                Missile missel = new Missile(textura, window, sourceRectangle, depth, shooterPosition, TargetPosition, shooterRotation, "cima");
                listaMisseis.Add(missel);

                sourceRectangle = new Rectangle(140, 60, 100, 50);

                Missile missel2 = new Missile(textura, window, sourceRectangle, depth, shooterPosition, TargetPosition, shooterRotation, "baixo");
                listaMisseis.Add(missel2);
            }
            else if (missileType == 6)
            {
                Rectangle sourceRectangle = new Rectangle(0, 60, 140, 50);

                Missile missel = new Missile(textura, window, sourceRectangle, depth, shooterPosition, TargetPosition, shooterRotation, "cima");
                listaMisseis.Add(missel);

                sourceRectangle = new Rectangle(140, 60, 100, 50);

                Missile missel2 = new Missile(textura, window, sourceRectangle, depth, shooterPosition, TargetPosition, shooterRotation, "baixo");
                listaMisseis.Add(missel2);
            }
            else if (missileType == 7)
            {
                Rectangle sourceRectangle = new Rectangle(0, 60, 140, 50);

                Missile missel = new Missile(textura, window, sourceRectangle, depth, shooterPosition, TargetPosition, shooterRotation, "cima");
                listaMisseis.Add(missel);

                sourceRectangle = new Rectangle(140, 60, 100, 50);

                Missile missel2 = new Missile(textura, window, sourceRectangle, depth, shooterPosition, TargetPosition, shooterRotation, "baixo");
                listaMisseis.Add(missel2);
            }
        }

        public static void Update(GameTime gameTime)
        {
            for (int i = 0; i < listaMisseis.Count; i++)
            {
                listaMisseis[i].update(gameTime);
            }
        }

        public void update(GameTime gameTime)
        {
            vel += aceleration;
            this.x += (float)(Math.Cos(((this.rotation) * Math.PI) / 180) * vel);
            this.y += (float)(Math.Sin(((this.rotation) * Math.PI) / 180) * vel);

            if ((this.y > window.ClientBounds.Height + 150) || (this.y < -150)) 
            {
                listaMisseis.Remove(this);
            }
            else if ((this.x > window.ClientBounds.Width + 150) || (this.x < -150))
            {
                listaMisseis.Remove(this);
            }
            

        }




        public static void Draw(GameTime gameTime, SpriteBatch spriteBatch)
        {
            for (int i = 0; i < listaMisseis.Count; i++)
            {
                listaMisseis[i].draw(gameTime, spriteBatch);

                if ((listaMisseis[i].rotation > -90) && (listaMisseis[i].rotation < 90))
                {
                    listaMisseis[i].spriteEffects = SpriteEffects.None;
                }
                else
                {
                    listaMisseis[i].spriteEffects = SpriteEffects.FlipHorizontally | SpriteEffects.FlipVertically;
                }
            }
        }
    }
}
